﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.Enterprise;
using RedDragon.Common.Domain.Outboundorder;
using RedDragon.Common.Domain.warehouse;
using RedDragon.Common.Dtos;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.ERP.ErroCodeEnum;
using SqlSugar;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Application.Commands.OutboundOrder
{
    /// <summary>
    /// 根据出货单ID查询出货信息Handler
    /// </summary>
    public class SearchOutboundIdHandler : IRequestHandler<SearchOutboundIdCommand, Result<OutbounddDto>>
    {
        private readonly ISqlSugarClient _db;
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 出库表
        /// </summary>
        public IBaseRepository<Outbound> outboundrepository;
        /// <summary>
        /// 部门仓储
        /// </summary>
        private readonly IBaseRepository<Departments> departmentrepository;
        /// <summary>
        /// 仓库表
        /// </summary>
        public IBaseRepository<Warehouse> warehouseRepository;
        /// <summary>
        /// 出库类型表
        /// </summary>
        public IBaseRepository<OutboundType> outboundTyperepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchOutboundIdHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        /// <param name="mapper"></param>
        /// <param name="outboundrepository"></param>
        /// <param name="departmentrepository"></param>
        /// <param name="warehouseRepository"></param>
        /// <param name="outboundTyperepository"></param>
        /// <param name="logger"></param>
        public SearchOutboundIdHandler(ISqlSugarClient db, IMapper mapper, IBaseRepository<Outbound> outboundrepository, IBaseRepository<Departments> departmentrepository, IBaseRepository<Warehouse> warehouseRepository, IBaseRepository<OutboundType> outboundTyperepository, ILogger<SearchOutboundIdHandler> logger)
        {
            _db = db;
            this.mapper = mapper;
            this.outboundrepository = outboundrepository;
            this.departmentrepository = departmentrepository;
            this.warehouseRepository = warehouseRepository;
            this.outboundTyperepository = outboundTyperepository;
            this.logger = logger;
        }

        /// <summary>
        /// 根据出货单ID查询出货信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result<OutbounddDto>> Handle(SearchOutboundIdCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<OutbounddDto>() { Code = (int)CommonErrorCode.Success };
            try
            {
                var query = await _db.Queryable<Outbound, Departments, Warehouse, OutboundType>((a, b, c, d) => new JoinQueryInfos(
                    JoinType.Inner, a.DepartmentId == b.DepartmentId,
                    JoinType.Inner, a.WarehouseId == c.WarehouseId,
                    JoinType.Inner, a.OutboundTypeId == d.OutboundTypeId
                    ))
                    .Select((a, b, c, d) => new OutbounddDto()
                    {
                        OutboundId = a.OutboundId,
                        OutboundCode = a.OutboundCode,
                        OutboundTypeId = a.OutboundTypeId,
                        OutboundTypeName = d.OutboundTypeName,
                        WarehouseId = a.WarehouseId,
                        WarehouseName = c.WarehouseName,
                        OutboundDec = a.OutboundDec,
                        SalesNo = a.SalesNo,
                        OutboundDate = a.OutboundDate,
                        OutboundStatus = a.OutboundStatus,
                        SingleMaker = a.SingleMaker,
                        DepartmentId = a.DepartmentId,
                        DepartmentName = b.DepartmentName,
                        IsStatus = a.IsStatus,
                        IsDel = a.IsDel,
                        AuditStatus = a.AuditStatus,
                        CreatedOwner = a.CreatedOwner,
                        CreatedTime = a.CreatedTime
                    }).Where(a => a.IsDel == 1&&a.OutboundId==request.OutboundId).FirstAsync();
                result.Data = query;
                result.Msg = "根据出货单ID查询出货信息成功";
            }
            catch (Exception ex)
            {
                result.Msg = "根据出货单ID查询出货信息失败" + ex.Message;
                logger.LogError("根据出货单ID查询出货信息失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
